<?php
use GatewayClient\Gateway;
global $_GPC, $_W;
    $uniacid = $_W['uniacid'];

    $rid = intval($_GPC['rid']);

    $count = intval($_GPC['count']);

    $pici = intval($_GPC['pici']);

    $c = 100001;

    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    if (strpos($user_agent, 'MicroMessenger') === false) {
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: {$this->createMobileUrl('other',array('type'=>1,'id'=>$rid))}");
        exit();
    }

    $from_user = $_W['fans']['from_user'];
    $uniacid = $_W['uniacid'];
    load()->model('account');
    $_W['account'] = account_fetch($_W['acid']);
    $cookieid = '__cookie_haoman_dpm_201606186_' . $rid;
    $cookie = json_decode(base64_decode($_COOKIE[$cookieid]),true);
    if ($_W['account']['level'] != 4) {
        $from_user = $cookie['openid'];
    }
////    $from_user = 'oQAFAwCS19dHrsZhSd4h0uRdEKUM';
//


    $countmoneyreply = pdo_fetch( " SELECT * FROM ".tablename('haoman_dpm_countmoneyreply')." WHERE rid='".$rid."' " );

    if($countmoneyreply['iscount'] != 1){
        $data = array(
            'shenyu' => $count,
            'msg' => '未开启数钱功能，请先后台开启！'.$rid,
            'code' => 11,
        );
        echo json_encode($data);
        exit;
    }

    if($countmoneyreply['pici'] != $pici){
        $data = array(
            'shenyu' => 0,
            'msg' => '活动已开始，刷新后参与',
            'status' => 0,
            'code' => 11,
        );
        echo json_encode($data);
        exit;
    }

    if($countmoneyreply['status'] == 0){
        $data = array(
            'msg' => '活动未开始',
            'code' => 11
        );
        echo json_encode($data);
        exit;
    }
    if($countmoneyreply['status'] == 2){
        $data = array(
            'msg' => '活动已经结束',
            'code' => 13
        );
        echo json_encode($data);
        exit;
    }
    if($countmoneyreply['status'] == 1&&$count<1){
        $data = array(
            'msg' => '活动已开始',
            'code' => 5
        );
        echo json_encode($data);
        exit;
    }

//    $fans = pdo_fetch("select id,from_user,is_online from " . tablename('haoman_dpm_fans') . " where rid =:rid and from_user = :from_user ", array(':rid'=>$rid,':from_user' => $from_user));
    $create_time = pdo_fetchcolumn( " SELECT createtime FROM ".tablename('haoman_dpm_countmoneyreply')." WHERE rid='".$rid."' and pici =  '".$pici."' order by id desc" );

    $user= pdo_fetch( " SELECT id FROM ".tablename('haoman_dpm_countmoneyuser')." WHERE rid='".$rid."' and pici =  '".$pici."' and from_user = '".$from_user."'" );

    pdo_update('haoman_dpm_countmoneyuser', array('createtime'=>$create_time,'count'=>$count,'endtime' => time()), array('id' => $user['id']));

//    $message = [
//        'type' => 'logMessage2',
//        'data' => $count
//    ];
//    $uid = $from_user.$rid;
//    $uid2 = $rid.$pici.$uniacid*$c;
//
//    Gateway::sendToUid($uid2, json_encode($message));
